<?php defined('SYSPATH') or die('No direct script access.');
/**
 * controller for flickrwijit
 *
 * PHP version 5
 * LICENSE: This source file is subject to LGPL license 
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/copyleft/lesser.html
 * @author	   Ushahidi Team <team@ushahidi.com> 
 * @package	   Ushahidi - http://source.ushahididev.com
 * @copyright  Ushahidi - http://www.ushahidi.com
 * @license	   http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL) 
 */
class Adminsection_Controller extends Admin_Controller {
	
	public function __construct() 
	{
		parent::__construct();

		$this->template->this_page = 'adminsection';
		
	}
	
	public function index() {
		
		$this->template->content = new View('admin/manage/adminsection');
		
		// setup and initialize form field names
		$form = array
	    (
	    	'action' => '',
	    	'adminsection_id' => '',
			'adminsection_title' => '',
	    );
        //  Copy the form as errors, so the errors will be stored with keys
        //  corresponding to the form field names
        $errors = $form;
		$form_error = FALSE;
		$form_saved = FALSE;
		$form_action = "";
		// check, has the form been submitted, if so, setup validation
	    if ($_POST)
	    {
	    	// Instantiate Validation, use $post, so we don't overwrite $_POST
            // fields with our own things
            $post = new Validation($_POST);

            // Add some filters
	        $post->pre_filter('trim', TRUE);

	        if($post->action == 'a' ) {
	        	$post->add_rules('adminsection_title','required','length[3,60]');
	        }
	        
			// passed validation test.
			if($post->validate()) {
				$adminsection_id = $post->adminsection_id;
				
				$adminsection = new Adminsection_Model($adminsection_id);
				//Delete action
				if( $post->action == 'd'){
					
					$adminsection->delete( $adminsection_id );
					$form_saved = TRUE;
					$form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
				}
				
				else if($post->action == 'v') {
				// Show/Hide Action
	            	if ($adminsection->loaded==true)
					{
						if ($adminsection->adminsection_active == 1) {
							$adminsection->adminsection_active = 0;
							
						}
						else {
							$adminsection->adminsection_active = 1;
						}
						$adminsection->save();
						$form_saved = TRUE;
						$form_action = strtoupper(Kohana::lang('ui_admin.modified'));
					}
				}
				
				else if( $post->action == 'a' ) 		
				{ // Save Action
					$adminsection->adminsection_title = $post->adminsection_title;
					$adminsection->save();
					$form_saved = TRUE;
					$form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
				}
				
			} 
			
			else 
			{
				// repopulate the form fields
	            $form = arr::overwrite($form, $post->as_array());

	            // populate the error fields, if any
	            $errors = arr::overwrite($errors, $post->errors('adminsection'));
				$form_error = TRUE;	
			}
	    
	    }

	   // Pagination
        $pagination = new Pagination(array(
                            'query_string' => 'page',
                            'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'),
                            'total_items'    =>
 							ORM::factory('adminsection')->count_all()
                     	));

        $adminsections = ORM::factory('adminsection')
                        ->orderby('adminsection_title', 'asc')
                        ->find_all((int) Kohana::config('settings.items_per_page_admin'), 
                            $pagination->sql_offset);

        $this->template->content->form = $form;
		$this->template->content->form_error = $form_error;
        $this->template->content->form_saved = $form_saved;
		$this->template->content->form_action = $form_action;
        $this->template->content->pagination = $pagination;
        $this->template->content->total_items = $pagination->total_items;
        $this->template->content->adminsections = $adminsections;
		$this->template->content->errors = $errors;

        // Javascript Header
        $this->template->colorpicker_enabled = TRUE;
        $this->template->js = new View('admin/manage/adminsection_js');
	}
}
